package per.sunmes.cl.g.actor;

import per.sunmes.cl.g.data.type.Direction;
import per.sunmes.cl.g.sys.U;

import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class Zone extends Group {
	public static final String LAND_0 = "dialog/land_0.png";

	Image landPic;
	Player player;
	Card landCard;
	Zone[][] zoneMap;
	int zoneX, zoneY;

	public Zone() {
		setTransform(false);

		landPic = U.image(LAND_0);
		addActor(landPic);
		setSize(landPic.getWidth(), landPic.getHeight());
	}

	public Zone getSurround(Direction direct) {
		return getDirection(direct, 1);
	}

	public Zone getDirection(Direction direct, int len) {
		switch (direct) {
		case Down:
			if (zoneY - len >= 0) {
				return zoneMap[zoneX][zoneY - len];
			}
			break;
		case Left:
			if (zoneX - len >= 0) {
				return zoneMap[zoneX - len][zoneY];
			}
			break;
		case LeftDown:
			if (zoneX - len >= 0 && zoneY - len >= 0) {
				return zoneMap[zoneX - len][zoneY - len];
			}
			break;
		case LeftUp:
			if (zoneX - len >= 0 && zoneY + len < zoneMap[zoneX].length) {
				return zoneMap[zoneX - len][zoneY + len];
			}
			break;
		case Right:
			if (zoneX + len < zoneMap.length) {
				return zoneMap[zoneX + len][zoneY];
			}
			break;
		case RightDown:
			if (zoneX + len < zoneMap.length && zoneY - len >= 0) {
				return zoneMap[zoneX + len][zoneY - len];
			}
			break;
		case RightUp:
			if (zoneX + len < zoneMap.length
					&& zoneY + len < zoneMap[zoneX].length) {
				return zoneMap[zoneX + len][zoneY + len];
			}
			break;
		case Up:
			if (zoneY + len < zoneMap[zoneX].length) {
				return zoneMap[zoneX][zoneY + len];
			}
			break;
		default:
			break;
		}
		return null;
	}

	public int getZoneX() {
		return zoneX;
	}

	public void setZoneX(int zoneX) {
		this.zoneX = zoneX;
	}

	public int getZoneY() {
		return zoneY;
	}

	public void setZoneY(int zoneY) {
		this.zoneY = zoneY;
	}

	public Zone[][] getZoneMap() {
		return zoneMap;
	}

	public void setZoneMap(Zone[][] zoneMap) {
		this.zoneMap = zoneMap;
	}

	public Card getCard() {
		return landCard;
	}

	public void setCard(Card landCard) {
		this.landCard = landCard;
	}

	public Player getPlayer() {
		return player;
	}

	public void setPlayer(Player player) {
		this.player = player;
		if (player == null) {
			landPic.setDrawable(new TextureRegionDrawable(U.region(LAND_0)));
		} else {
			landPic.setDrawable(new TextureRegionDrawable(U
					.region(player.landPicPath)));
		}
	}

	@Override
	public String toString() {
		return String.format("Zone[%d]{XY[%d,%d] }", hashCode(), zoneX, zoneY);
	}
}
